package com.atguigu.test5.test51;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

/**
 * @author 路飞
 * @date 2024/4/10 10:22
 * @description 3.	问题：某班30个学生的学号为20070301-20070330,
 * 给出所有同学的成绩（可用随机数产生，范围60-100），
 * 请编写程序将本班各位同学的成绩按照从低到高排序打印输出，
 * 如果成绩相同，则按照学号升序排列。
 */
public class Test3 {
    public static void main(String[] args) {
        Student student;
        Random random = new Random();
        List<Student> studentList = new ArrayList<>();
        for (int i = 20070301; i <= 20070330; i++) {
            student = new Student(i, random.nextInt(60, 100));
            studentList.add(student);
        }
        System.out.println("排序前：");
        studentList.forEach(System.out::println);

        Collections.sort(studentList);

        System.out.println("排序后：");
        studentList.forEach(System.out::println);
    }
}

class Student implements Comparable<Student>{
    private int stuNo;
    private double score;

    @Override
    public String toString() {
        return "Student{" +
                "stuNo='" + stuNo + '\'' +
                ", score=" + score +
                '}';
    }

    public int getStuNo() {
        return stuNo;
    }

    public void setStuNo(int stuNo) {
        this.stuNo = stuNo;
    }

    public double getScore() {
        return score;
    }

    public void setScore(double score) {
        this.score = score;
    }

    public Student(int stuNo, double score) {
        this.stuNo = stuNo;
        this.score = score;
    }

    public Student() {
    }

    @Override
    public int compareTo(Student o) {
        if (this.score == o.getScore()) {
            return this.getStuNo() - o.getStuNo();
        }
        return (int) (this.score - o.score);
    }
}